﻿Public Class MASDISTRICT
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASDISTRICT As New PROVIDERSCLASS

    Private Sub codeAmpurTextbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CODEAMPURTxt.KeyPress
        If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = True
        End If
    End Sub

    Private Sub filterCountry_Click(sender As Object, e As EventArgs) Handles filterCountry.Click
        filterCountry.Text = ""
    End Sub

    Private Sub filterProvince_Click(sender As Object, e As EventArgs) Handles filterProvince.Click
        filterProvince.Text = ""
    End Sub

    Private Sub filterCountry_TextChanged(sender As Object, e As EventArgs) Handles filterCountry.TextChanged
        If filterCountry.Text.ToString.Length > 0 Then
            filterProvince.Enabled = True
            Dim s2 As FILTERCLASS
            Dim sql2 As String = "SELECT CAST(codechangwat AS CHAR(8)) AS CODECHANGWAT, `changwat` FROM maschangwat WHERE codecountry1 = '" & filterCountry.Tag & "'"
            s2 = New FILTERCLASS(filterProvince, sql2, "รหัสจังหวัด,ชื่อจังหวัด", "50,160", "1,1", "1,1")
            's.SetShowInDown = False
            s2.SetShowBorder = True
        Else
            filterProvince.Enabled = False
        End If
    End Sub

    Private Sub DISTRICT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DesignForm(Me)
        DesignGrid(MASAMPURGrid)
        selectDataGridAmpur()

        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(codecountry AS CHAR(8)) AS CODECOUNTRY, country FROM mascountry"
        s = New FILTERCLASS(filterCountry, sql, "รหัสประเทศ,ชื่อประเทศ", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        PROVIDER_MASDISTRICT.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))

        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Private Sub selectDataGridAmpur()
        Dim objSelectDatamasAmpur As New prated
        Dim MASAMPUR As DataTable
        MASAMPUR = connect.GetTable(objSelectDatamasAmpur.selectAmpur_)
        MASAMPURGrid.PrimaryGrid.DataSource = MASAMPUR
    End Sub

    Private Sub MASAMPURGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASAMPURGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value() IsNot DBNull.Value Then
            AMPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value()
        Else
            AMPIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสอำเภอ").Value() IsNot DBNull.Value Then
            CODEAMPURTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสอำเภอ").Value()
        Else
            CODEAMPURTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value() IsNot DBNull.Value Then
            AMPURTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            AMPURTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value() IsNot DBNull.Value Then
            filterCountry.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value()
            filterCountry.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY").Value()
        Else
            filterCountry.Text = ""
            filterCountry.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value() IsNot DBNull.Value Then
            filterProvince.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
            filterProvince.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECHANGWAT").Value()
        Else
            filterProvince.Text = ""
            filterProvince.Tag = ""
        End If
        addAmpur.Enabled = False
    End Sub

    Private Sub addAmpur_Click_1(sender As Object, e As EventArgs) Handles addAmpur.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASDISTRICT.PADD_ = True Then
                If filterCountry.Text.ToString.Length > 0 And filterProvince.Text.ToString.Length > 0 And CODEAMPURTxt.Text.Length > 0 And AMPURTxt.Text.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New prated
                    objInsertData.ampurCODEAMPUR_ = CInt(CODEAMPURTxt.Text)
                    objInsertData.ampurCODECHANGWAT_ = filterProvince.Tag
                    objInsertData.ampurCODECOUNTRY_ = filterCountry.Tag
                    objInsertData.ampurAMPUR_ = AMPURTxt.Text
                    objInsertData.insertAmpur()

                    selectDataGridAmpur()

                    'connectlocal.Dispose()

                    CODEAMPURTxt.Text = ""
                    AMPURTxt.Text = ""
                    filterCountry.Text = ""
                    filterCountry.Tag = ""
                    filterProvince.Text = ""
                    filterProvince.Tag = ""
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_MASDISTRICT.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editAmpur_Click_1(sender As Object, e As EventArgs) Handles editAmpur.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASDISTRICT.PUPD_ = True Then
                If filterCountry.Tag.ToString.Length > 0 And filterProvince.Tag.ToString.Length > 0 And CODEAMPURTxt.Text.Length > 0 And AMPURTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขอำเภอดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New prated
                        objEditData.ampurCODECHANGWAT_ = filterProvince.Tag
                        objEditData.ampurAMPUR_ = AMPURTxt.Text
                        objEditData.ampurCODECOUNTRY_ = filterCountry.Tag
                        objEditData.ampurCODEAMPUR_ = CInt(CODEAMPURTxt.Text)
                        objEditData.ampurAMPID_() = AMPIDTxt.Text
                        objEditData.editAmpur()

                        selectDataGridAmpur()

                        CODEAMPURTxt.Text = ""
                        AMPURTxt.Text = ""
                        filterCountry.Text = ""
                        filterCountry.Tag = ""
                        filterProvince.Text = ""
                        filterProvince.Tag = ""
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
                addAmpur.Enabled = True
            Else
                MsgBox(PROVIDER_MASDISTRICT.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masampur.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masampur.Text)
        nextform.ShowDialog()
    End Sub
End Class